#include "MulticastClient.h"

#include "../Common/GlobalError.h"

using namespace std;

/*---------------------------------------------------------------------------------------
--	FUNCTION:	MulticastClient::MulticastClient() : as()
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh, Renz Barron
--
--	PROGRAMMER:		Brad Paugh
--
--      INTERFACE:      PlaylistClient::PlaylistClient()
--
--	RETURNS: N/A
--
--	NOTES:
--		Constructor for the playlist client that starts up WSA
---------------------------------------------------------------------------------------*/
MulticastClient::MulticastClient() : as()
{
    WORD wVersionRequested = MAKEWORD(2,2);
    WSADATA WSAdata;
    WSAStartup(wVersionRequested, &WSAdata);
	stopped = false;
}

/*-----------------------------------------------------------------------------------------
--
--	FUNCTION: receivePacket
--
--	Date: March 25, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Brad Paugh
--
--	PROGRAMMER: Brad Paugh
--
--  INTERFACE: void MulticastClient::receivePacket(char* buffer, int packetSize)
--
--      char* buffer: Buffer containing the data from the server.
--
--      int packetSize: Size of the packet in bytes.
--
--  RETURNS: void
--
--	NOTES: Wrapper function for the 'RecvFrom' function. Receives a packet off of the
--         multicast stream.
--
-----------------------------------------------------------------------------------------*/
void MulticastClient::receivePacket(char* buffer, int packetSize)
{
    sock.RecvFrom(buffer, packetSize);
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlistclient::close ()
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Eric Rebeiro
--
--	PROGRAMMER:		Eric Rebeiro
--
--	RETURNS:		void	
--
--	NOTES:
--		Method to terminate client
---------------------------------------------------------------------------------------*/
void MulticastClient::close ()
{
}

/*----------------------------------------------------------------------------------------------------
--
--	FUNCTION: receiveFromStream
--
--	Date: March 25, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Brad Paugh, Renz Barron
--
--	PROGRAMMER: Brad Paugh, Renz Barron
--
--  INTERFACE: void MulticastClient::receiveFromStream(bool saveFile)
--
--      bool saveFile: Flag to indicate whether there is a file currently on the multicast
--                     stream.
--
--  RETURNS: void
--
--	NOTES: Creates/connects a socket, binds to the server's port, and connects to the multicasting
--         address. Starts the client streamin thread after.
--
----------------------------------------------------------------------------------------------------*/
void MulticastClient::receiveFromStream(bool saveFile)
{
    sock.Create("UDP");
    sock.Bind(MCPORT);
    sock.setMulticastClient("239.255.255.255");

    this->saveFile = saveFile;
    startRunning();
}

/*----------------------------------------------------------------------------------------------------
--
--	FUNCTION: stop
--
--	Date: March 25, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Brad Paugh
--
--	PROGRAMMER: Brad Paugh
--
--  INTERFACE: void MulticastClient::stop()
--
--  RETURNS: void
--
--	NOTES: Stops the multicast client.
--
----------------------------------------------------------------------------------------------------*/
void MulticastClient::stop()
{
	stopped = true;
}

/*----------------------------------------------------------------------------------------------------
--
--	FUNCTION: start
--
--	Date: March 25, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Brad Paugh
--
--	PROGRAMMER: Brad Paugh
--
--  INTERFACE: void MulticastClient::start()
--
--  RETURNS: void
--
--	NOTES: Starts the multicast client.
--
----------------------------------------------------------------------------------------------------*/
void MulticastClient::start()
{
	stopped = false;
}

/*----------------------------------------------------------------------------------------------------
--
--	FUNCTION: getAudioSystem
--
--	Date: March 25, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Brad Paugh
--
--	PROGRAMMER: Brad Paugh
--
--  INTERFACE: AudioSystem * MulticastClient::getAudioSystem ()
--
--  RETURNS: AudioSystem pointer.
--
--	NOTES: Returns a pointer to this class's AudioSystem object.
--
----------------------------------------------------------------------------------------------------*/
AudioSystem * MulticastClient::getAudioSystem ()
{
    return &as;
}

/*----------------------------------------------------------------------------------------------------
--
--	FUNCTION: run
--
--	Date: March 25, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Brad Paugh
--
--	PROGRAMMER: Brad Paugh
--
--  INTERFACE: void MulticastClient::run()
--
--  RETURNS: void
--
--	NOTES: The multicast server's main function. Receives a packet from the multicast stream then
--         passes it to its AudioSystem object so that the actual sound data can be extracted and
--         then be played.
--
----------------------------------------------------------------------------------------------------*/
void MulticastClient::run()
{
    char* buffer = (char*)malloc(BUFSIZE);
    char* data = (char*)malloc(BUFSIZE);
    while(1)
    {
        receivePacket(buffer, BUFSIZE);
        data = as.extractHeaderFromPacket(buffer);
		if (!stopped)
			as.playBuffer(data, BUFSIZE - HEADERSIZE);
		if (closed)
			break;
    }
}
